/**
  * <br>JAVACC DEMO 1.0<br>
  * <br>
  * @Package com.apache.dict.controller
*/
package com.apache.dict.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.dict.entity.DataItem;
import com.apache.dict.manager.DataItemManager;
import com.apache.dict.vo.ItemListVo;
import com.apache.uct.common.BaseAction;

/**
  * description: 数据字典条目action
  * @author Hou DaYu 创建时间：上午08:43:55
  */
@Controller
@RequestMapping("/dict/item/")
public class DataItemAction extends BaseAction<DataItem> {

	/**
	 * 数据字典条目管理的Service
	 */
	@Autowired
	DataItemManager dataItemManager;

	/**
	 * 进入数据字典列表页面
	 */
	@RequestMapping("tree.action")
	public ModelAndView initItemTree(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dict/mvc/data-item-tree");
		String cateEname = request.getParameter("cateEname");//科目标识
		request.setAttribute("cateEname", cateEname);
		return view;
	}

	/**
	 * 获取列表，数据字典查询;参数itemText，itemValue,id,cateEname
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("list!tree.action")
	@ResponseBody
	public net.sf.json.JSONArray doTree(HttpServletRequest request,
			@RequestParam(value = "cateEname", required = true) String cateEname) {
		//String cateEname = request.getParameter("cateEname");//科目标识
		String itemText = request.getParameter("itemText");//条目名称
		String itemValue = request.getParameter("itemValue");//条目标识
		String fatherId = request.getParameter("id");//父节点ID，如果某节点为父节点，则fatherId=0
		String fatherValue = request.getParameter("fatherValue");

		ItemListVo item = new ItemListVo();
		ParamsVo vo = new ParamsVo();
		item.setCateEname(cateEname);

		if (Validator.isNotNull(fatherId)) {
			item.setFatherId(fatherId);
		} else if (Validator.isNotNull(fatherValue)) {
			item.setFatherValue(fatherValue);
		} else {
			item.setFatherId("0");
		}

		item.setItemText(itemText);
		item.setItemValue(itemValue);
		vo.setObj(item);
		List<DataItem> list = dataItemManager.getList(vo);
		net.sf.json.JSONArray jary = new net.sf.json.JSONArray();
		for (int i = 0; i < list.size(); i++) {
			DataItem ditem = list.get(i);
			net.sf.json.JSONObject jvo = net.sf.json.JSONObject.fromObject(ditem);
			if (ditem.getSubCount() != null && ditem.getSubCount() > 0) {
				jvo.put("state", "closed");
			} else {
				jvo.put("state", "opened");
			}
			jary.add(jvo);
		}
		request.setAttribute("cateEname", cateEname);
		return jary;
	}

	/**
	 * 初始化信息，跳转到新增或修改页面
	 * @param activityId
	 * @return
	 */
	@Token(save = true)
	@RequestMapping("add.action")
	public ModelAndView add(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(ADD);
		DataItem dataItem = new DataItem();
		dataItem.setItemStatus("1");
		dataItem.setSubCount(Integer.valueOf(0));
		String fatherId = request.getParameter("fatherId");
		if (Validator.isNotNull(fatherId)) {
			dataItem.setFatherId(fatherId);
		}
		String cateEname = request.getParameter("cateEname");//科目标识
		if (Validator.isNotNull(cateEname)) {
			dataItem.setCateEname(cateEname);
		}
		if (Validator.isEmpty(dataItem.getSubCount())) {
			dataItem.setSubCount(0);
		}
		view.addObject("item", dataItem);
		return view;
	}

	/**
	 * 初始化信息，跳转到新增或修改页面
	 * @param activityId
	 * @return
	 */
	@Token(save = true)
	@RequestMapping("edit.action")
	public ModelAndView edit(HttpServletRequest request, String id) {
		ModelAndView view = new ModelAndView(EDIT);
		DataItem dataItem = null;
		ParamsVo<DataItem> vo = new ParamsVo<DataItem>();
		vo.setInfoId(id);
		dataItem = (DataItem) dataItemManager.getInfoById(vo);
		if (Validator.isEmpty(dataItem.getSubCount())) {
			dataItem.setSubCount(0);
		}
		view.addObject("item", dataItem);
		return view;
	}

	@Token(remove = true)
	@RequestMapping("save.action")
	public @ResponseBody
	ResultMsg save(HttpServletRequest request, HttpServletResponse response, DataItem dataItem) {
		ResultMsg rmsg = new ResultMsg();
		rmsg.setFlag("T");
		rmsg.setMsg("操作成功！");
		response.setContentType("text/html");
		String itemId = dataItem.getItemId();
		//		String cateEname = request.getParameter("cateEname");

		ParamsVo<DataItem> vo = this.getParamsVo(request);
		String fatherId = request.getParameter("fatherId");
		//		String itemRemark = request.getParameter("itemRemark");
		//		String itemStatus = request.getParameter("itemStatus");
		//		String itemValue = request.getParameter("itemValue");
		//		String itemText = request.getParameter("itemText");

		if (Validator.isNull(itemId)) {//新增
			ParamsVo<DataItem> pvo = new ParamsVo<DataItem>();
			pvo.setMethodKey("checkCateEnameAndItemValue");
			pvo.setParams("cateEname", dataItem.getCateEname());
			pvo.setParams("itemValue", dataItem.getItemValue());
			Object obj = dataItemManager.execute(pvo);
			if ("true".equals(obj.toString())) {
				rmsg.setFlag("F");
				rmsg.setMsg("新增条目值已经存在！");
				/** 输出结果 */
				return rmsg;
			}
			vo.setObj(dataItem);
			itemId = dataItemManager.saveInfo(vo);
			if (Validator.isNull(itemId)) {
				rmsg.setFlag("F");
				rmsg.setMsg("新增条目失败，请重试！");
			}
		} else {//修改
			dataItem.setSubCount(Integer.parseInt(Validator.getDefaultStr(dataItem.getSubCount() + "", "0")));
			vo.setObj(dataItem);
			dataItemManager.editInfo(vo);
		}
		return rmsg;
	}

	/**
	 * 删除数据字典
	 */
	@RequestMapping("del.action")
	@ResponseBody
	public ResultMsg delete(HttpServletRequest request, String id) {
		ResultMsg rmsg = new ResultMsg();
		String cateEname = request.getParameter("cateEname");
		rmsg.setFlag("T");
		rmsg.setMsg("操作成功！");
		if (Validator.isNotNull(id) && Validator.isNotNull(cateEname)) {
			ParamsVo<DataItem> itemvo = new ParamsVo<DataItem>();
			itemvo.setMethodKey("checkDataItemByFatherId");
			itemvo.setParams("cateEname", cateEname);
			itemvo.setParams("fatherId", id);
			Object result = dataItemManager.execute(itemvo);
			if ("true".equals(result.toString())) {
				rmsg.setFlag("H");
				rmsg.setMsg("请先删除该科目下的条目列表！");
				return rmsg;
			}
		}
		if (Validator.isNotNull(id)) {
			ParamsVo<DataItem> vo = new ParamsVo<DataItem>();
			vo.setInfoId(id);
			dataItemManager.deleteInfo(vo);
		}
		return rmsg;
	}

	/**
	 * 获取分页列表，数据字典查询
	 */
	@RequestMapping("list!data.action")
	@ResponseBody
	protected Object data(HttpServletRequest request, String page, String rows) {
		String cateEname = request.getParameter("cateEname");//科目标识
		String itemText = request.getParameter("itemText");//条目名称
		String itemValue = request.getParameter("itemValue");//条目标识
		ParamsVo vo = new ParamsVo();
		vo.setParams("pageSize", Validator.getDefaultStr(rows, "10"));
		vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
		ItemListVo item = new ItemListVo();
		item.setCateEname(cateEname);
		item.setItemText(itemText);
		item.setItemValue(itemValue);
		vo.setObj(item);
		Page pageList = dataItemManager.getPageInfo(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", pageList.getCount());
		mapData.put("rows", pageList.getPageObjects());
		request.setAttribute("cateEname", cateEname);
		return mapData;
	}

	@Override
	protected BaseManager<DataItem> getBaseManager() {
		return dataItemManager;
	}

	@Override
	protected String getPrefix() {
		return "dict/mvc/data-item-";
	}

}
